package com.zxl.live.screen.screen.ui.helper;

import android.graphics.BitmapFactory;
import android.media.RingtoneManager;
import android.net.Uri;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.zxl.live.screen.R;
import com.zxl.live.screen.screen.ui.helper.data.SuiScreenDataHelper;
import com.zxl.live.screen.screen.ui.helper.effect.BaseEffectHelper;
import com.zxl.live.screen.screen.ui.helper.window.WindowHelper;
import com.zxl.live.screen.screen.ui.receiver.ScreenReceiver;
import com.zxl.live.screen.screen.ui.service.ScreenService;
import com.zxl.live.screen.screen.ui.widget.anim.BaseAnimationView;
import com.zxl.live.screen.ui.activity.SplashActivity;
import com.zxl.live.tools.common.ApplicationHelper;
import com.zxl.live.tools.tools.TimeUtils;
import com.zxl.live.tools.track.StatisticsConstant;

/**
 * Created by Administrator on 2018/1/10.
 */
public class SuiScreenViewHelper extends BaseEffectHelper implements Runnable, ScreenReceiver.ScreenStatusListener {

    private View mTipsView;

    private View mShowView;

    private static final int SYSTEM_ERROR_ARRAY[] = {R.layout.widget_screen_lanping1, R.layout.widget_screen_lanping2};

    private static final int ANIMATION_ARRAY[] = { R.layout.widget_screen_anim_1,  R.layout.widget_screen_anim_2, R.layout.widget_screen_anim_3,
            R.layout.widget_screen_anim_4, R.layout.widget_screen_anim_5,  R.layout.widget_screen_anim_6, R.layout.widget_screen_anim_7 };

    public SuiScreenViewHelper(ScreenService mContext) {
        super(mContext);
        ScreenReceiver.registerListener(this);
    }

    @Override
    protected boolean initSuiData(final int timer) {
        String path = DataHelper.getDownloadPath(dataType, position);
        if (!TextUtils.isEmpty(path)) {
            try {
                if (dataType == SuiScreenDataHelper.DataType.ANIMATION) {
                    mShowView = LayoutInflater.from(mContext).inflate(ANIMATION_ARRAY[position], null);
                    if (BaseAnimationView.class.isInstance(mShowView)) {
                        BaseAnimationView animationView = (BaseAnimationView) mShowView;
                        animationView.setData(path);
                    } else {
                        return false;
                    }
                } else {
                    mShowView = LayoutInflater.from(mContext).inflate(R.layout.window_sui_screen, null);
                    ImageView imageView = (ImageView) mShowView.findViewById(R.id.src);
                    imageView.setImageBitmap(BitmapFactory.decodeFile(path));
                }
            } catch (Throwable e) {
                e.printStackTrace();
                dataType.upload(mContext, StatisticsConstant.Event.ERROR, position + "");
                Toast.makeText(mContext, "特效使用失败请重试", Toast.LENGTH_SHORT).show();
                return false;
            }
        } else if (dataType == SuiScreenDataHelper.DataType.SYSTEM_ERROR && position < 2) {
            mShowView = LayoutInflater.from(mContext).inflate(SYSTEM_ERROR_ARRAY[position], null);
        } else {
            Toast.makeText(mContext, "特效使用失败请重试", Toast.LENGTH_SHORT).show();
            return false;
        }
        ApplicationHelper.postUiThreadDelayed(new Runnable() {
            @Override
            public void run() {
                WindowHelper.getWindowHelper().removeView(mTipsView);
                ApplicationHelper.postUiThreadDelayed(SuiScreenViewHelper.this, Math.max(1, timer - 2) * TimeUtils.SECOND);
            }
        }, 2000);
        return true;
    }

    @Override
    protected void showTips(int timer) {
        if (null == mTipsView) {
            mTipsView = LayoutInflater.from(mContext).inflate(R.layout.windown_toast_tips, null);
        } else {
            if (mTipsView.getParent() != null) {
                ((ViewGroup) mTipsView.getParent()).removeView(mTipsView);
            }
        }
        TextView textView = (TextView) mTipsView.findViewById(R.id.tv_toast_content);
        textView.setText(String.format(mContext.getResources().getString(dataType.getTips(), timer + "")));
        WindowHelper.getWindowHelper().addView(mTipsView);
    }

    @Override
    public void onDismiss() {
        if (null != mShowView) {
            WindowHelper.getWindowHelper().removeView(mShowView);
            SplashActivity.startSplash(mContext, dataType, position);
            mShowView = null;
        }
    }

    public void playMusic() {
        try {
            Uri localUri = Uri.parse("android.resource://" + ApplicationHelper.getContext().getPackageName() + "/" + R.raw.kongbu);
            RingtoneManager.getRingtone(ApplicationHelper.getContext().getApplicationContext(), localUri).play();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        if (dataType == SuiScreenDataHelper.DataType.KUONGBU) {
            playMusic();
            ApplicationHelper.postUiThreadDelayed(new Runnable() {
                @Override
                public void run() {
                    registerListener();
                    WindowHelper.getWindowHelper().addView(mShowView);
                }
            }, 3040L);
        } else {
            registerListener();
            WindowHelper.getWindowHelper().addView(mShowView);
        }
    }


    @Override
    public void onScreenOn() {
        if (BaseAnimationView.class.isInstance(mShowView)) {
            BaseAnimationView animationView = (BaseAnimationView) mShowView;
            animationView.start();
        }
    }

    @Override
    public void onScreenOff() {
        if (BaseAnimationView.class.isInstance(mShowView)) {
            BaseAnimationView animationView = (BaseAnimationView) mShowView;
            animationView.stop();
        }
    }
}
